/**
 * \file XMLParser.cpp
 * \brief XMLParser definition class
 * \author Edouard ROGE
 * \date 16 October 2012
 */

#include "File.h"
#include "XMLNode.h"
#include "XMLParser.h"

namespace Core
{
	namespace File
	{
		XMLParser::XMLParser()
			: m_pRoot(NULL)
		{
		}

		XMLParser::~XMLParser()
		{
			if (m_pRoot)
				delete m_pRoot;
		}

		XMLNode * XMLParser::Load(const char * _filename)
		{
			File file(_filename);
			if (file.Open(eFile_Read | eFile_Text))
			{
				m_pRoot = XMLNode::Load(file);
				file.Close();
			}
			return m_pRoot;
		}

		XMLNode * XMLParser::Load(const std::string & _filename)
		{
			File file(_filename);
			if (file.Open(eFile_Read | eFile_Text))
			{
				m_pRoot = XMLNode::Load(file);
				file.Close();
			}
			return m_pRoot;
		}

		XMLNode * XMLParser::Load(const URI & _filename)
		{
			File file(_filename);
			if (file.Open(eFile_Read | eFile_Text))
			{
				m_pRoot = XMLNode::Load(file);
				file.Close();
			}
			return m_pRoot;
		}

		error XMLParser::Save(const char * _filename)
		{
			if (!m_pRoot)
				return errFailed;

			File file(_filename);
			if (file.Open(eFile_Write | eFile_Text))
			{
				error err = m_pRoot->Save(file, 0);
				file.Close();
				return err;
			}

			return errFailed;
		}

		error XMLParser::Save(const std::string & _filename)
		{
			if (!m_pRoot)
				return errFailed;

			File file(_filename);
			if (file.Open(eFile_Write | eFile_Text))
			{
				error err = m_pRoot->Save(file, 0);
				file.Close();
				return err;
			}

			return errFailed;
		}

		error XMLParser::Save(const URI & _filename)
		{
			if (!m_pRoot)
				return errFailed;

			File file(_filename);
			if (file.Open(eFile_Write | eFile_Text))
			{
				error err = m_pRoot->Save(file, 0);
				file.Close();
				return err;
			}

			return errFailed;
		}

		void XMLParser::SetRootNode(XMLNode * _pNode)
		{
			m_pRoot = _pNode;
		}
	}
}
